package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yu.crm.domain.pojo.*;
import com.yu.crm.mapper.CrmUserRoleMapper;
import com.yu.crm.service.CrmDepartmentMemberService;
import com.yu.crm.service.CrmRoleGroupService;
import com.yu.crm.service.CrmRoleService;
import com.yu.crm.service.CrmUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.ExceptionHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmUserRoleServiceImpl extends ServiceImpl<CrmUserRoleMapper, CrmUserRole> implements CrmUserRoleService {
    @Autowired
    CrmDepartmentMemberService crmDepartmentMemberService;
    @Autowired
    CrmRoleGroupService crmRoleGroupService;
    @Autowired
    CrmRoleService crmRoleService;

    @Override
    public boolean saveBatch(List<CrmUserRole> entityList) {
        List<CrmUserRole> userRoleList = new ArrayList<>();
        for (CrmUserRole entity: entityList){
            ValidatorHelpers.notEmpty(entity.getUserId(), "用户ID不能为空");
            ValidatorHelpers.notEmpty(entity.getRoleId(), "角色ID不能为空");
            QueryWrapper<CrmUserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", entity.getUserId());
            queryWrapper.eq("role_id", entity.getRoleId());
            int oldCount = super.count(queryWrapper);
            if(oldCount <= 0){
                userRoleList.add(entity);
            }

            CrmDepartmentMember member=new CrmDepartmentMember();
            member.setGmtModified(LocalDateTime.now());
            if(DataHelpers.isNotEmpty(entity.getDepartmentId())){
                member.setDepartmentId(Long.parseLong(entity.getDepartmentId()));
            }
            if(DataHelpers.isNotEmpty(entity.getPositionId())){
                member.setDepartmentPositionId(Long.parseLong(entity.getPositionId()));
            }

            QueryWrapper<CrmDepartmentMember> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("user_id",entity.getUserId());
            queryWrapper1.eq("is_deleted",0);

            crmDepartmentMemberService.update(member,queryWrapper1);

        }
        if(userRoleList.size()>0){
            return ExceptionHelpers.checkData(super.saveBatch(userRoleList), "新增成功");
        }else{
            return true;
        }
    }

    @Override
    public boolean removeBatch(List<CrmUserRole> entityList) {
        for (CrmUserRole entity: entityList){
            QueryWrapper<CrmUserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", entity.getUserId());
            queryWrapper.eq("role_id", entity.getRoleId());
            ExceptionHelpers.checkData(baseMapper.delete(queryWrapper), "删除失败");
        }
        return true;
    }

    @Override
    public IPage<CrmUser> listUserByRoleId(IPage<CrmUser> page, Long roleId,String condition) {
        List<Long> roleIds=new ArrayList<>();

        List<Integer> roleTypes=new ArrayList<>();
        roleTypes.add(1);
        roleTypes.add(2);
        QueryWrapper<CrmRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_group_id",roleId);
        queryWrapper.in("role_type",roleTypes);
        queryWrapper.eq("is_deleted",0);

        List<CrmRole> list = crmRoleService.list(queryWrapper);
        if(DataHelpers.isNotEmpty(list)){
            roleIds= list.stream().map(CrmRole::getId).collect(Collectors.toList());
        }
        if(DataHelpers.isNotEmpty(roleId)){
            roleIds.add(roleId);
        }
        return baseMapper.listUserByRoleId(page, roleIds,condition);
    }
}
